package com.orangelife.mobile.parkingcard.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.TimeUtil;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.parkingcard.bean.ParkingUsersListBean;
import com.orangelife.mobile.parkingcard.biz.ParkingSpaceHttpBiz;


import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by HeYuCan on 2016/7/7.
 */
public class ParkingSpaceUsersAdapter extends FatherAdapter {

    private List<ParkingUsersListBean> mList = new ArrayList<>();
    private Context mContext;


    public ParkingSpaceUsersAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public BaseAdapter setList(List list) {
        this.mList = list;
        return null;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_parking_space_users, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

            ParkingUsersListBean parkingListUsersBean = mList.get(position);
            String userState = parkingListUsersBean.getState();
            String startTime = TimeUtil.timeStampToDate(Long.parseLong(parkingListUsersBean.getStartTime()), "yyyy/MM/dd");
            String endTime = TimeUtil.timeStampToDate(Long.parseLong(parkingListUsersBean.getEndTime()), "yyyy/MM/dd");
            String name = parkingListUsersBean.getName();
            String authorizationTime = TimeUtil.timeStampToDate(parkingListUsersBean.getAuthorizationTime(), "yyyy/MM/dd");
            String contact = parkingListUsersBean.getPhone();

            viewHolder.btnTerminateAuthor.setTag(R.id.btn_terminate_authorization, position);
            viewHolder.btnTerminateAuthor.setOnClickListener(listener);

            viewHolder.tvUserDuration.setText(startTime + "至" + endTime);
            viewHolder.tvUserName.setText(name);
            viewHolder.tvContact.setText(contact);
            viewHolder.tvAuthorizeTime.setText(authorizationTime);

            /*0表示将要使用,1表示当前有人使用,2为历史的使用记录*/
            if (userState.equals("0") || userState.equals("1")) {
                viewHolder.llTerminate.setVisibility(View.VISIBLE);
            } else if (userState.equals("2")) {
                viewHolder.llTerminate.setVisibility(View.GONE);
                viewHolder.llItem.setTag(R.id.llItem, position);
                viewHolder.llItem.setOnLongClickListener(longClickListener);
            }
        return convertView;
    }

    View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            int pos = (int) v.getTag(R.id.llItem);
            deleteUser(pos);
            return false;
        }
    };

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int pos = (int) v.getTag(R.id.btn_terminate_authorization);
            terminateAuthorization(pos);
        }
    };


    private void terminateAuthorization(final int pos) {
        final String linkAptHouseholderID=mList.get(pos).getLinkAptHouseholderID();
        final ConfirmDialog confirmDialog = new ConfirmDialog(mContext, "温馨提示", "您确定要终止对该使用者的授权？", "取消", "确定");
        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                confirmDialog.dismiss();
            }

            @Override
            public void doRight() {
                confirmDialog.dismiss();
                ((OrangeLifeBaseActivity)mContext).showDialog((OrangeLifeBaseActivity)mContext);
                ParkingSpaceHttpBiz.terminateAuthorization(linkAptHouseholderID, new VolleyHttpUtil.ResponseCallBack() {
                    @Override
                    public void handleResponse(JSONObject jsonObject, int i) {

                        Map<String, Object> map = new HashMap<>();
                        map = JSONHelper.jsonobjToMap(jsonObject);
                        String errInfo = String.valueOf(map.get("errInfo"));
                        ((OrangeLifeBaseActivity)mContext).closeDialog();
                        if (errInfo.equals("OK")) {
                            mList.get(pos).setState("2");
                            notifyDataSetChanged();

                        }else {
                            ToastHelper.getInstance()._toast(errInfo);
                        }

                    }
                });
            }
        });
        confirmDialog.setCanceledOnTouchOutside(false);
        confirmDialog.show();
    }


    private void deleteUser(final int pos) {
        final ConfirmDialog confirmDialog = new ConfirmDialog(mContext, "温馨提示", "您确定要删除该使用者？", "取消", "确定");
        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                confirmDialog.dismiss();
            }

            @Override
            public void doRight() {
                confirmDialog.dismiss();
                ((OrangeLifeBaseActivity)mContext).showDialog((OrangeLifeBaseActivity)mContext);
                String linkAptHouseholderID = mList.get(pos).getLinkAptHouseholderID();
                ParkingSpaceHttpBiz.deleteAuthorization(linkAptHouseholderID, new VolleyHttpUtil.ResponseCallBack() {
                    @Override
                    public void handleResponse(JSONObject jsonObject, int i) {
                        Map<String, Object> map = new HashMap<>();
                        map = JSONHelper.jsonobjToMap(jsonObject);
                        String errInfo = String.valueOf(map.get("errInfo"));
                        ((OrangeLifeBaseActivity)mContext).closeDialog();
                        if (errInfo.equals("OK")) {
                            mList.remove(pos);
                            setList(mList);
                            notifyDataSetChanged();
//                            notifyDataSetInvalidated();
                        }else {
                            ToastHelper.getInstance()._toast(errInfo);

                        }


                    }
                });
            }
        });
        confirmDialog.setCanceledOnTouchOutside(false);
        confirmDialog.show();
    }



    class ViewHolder {
        public ViewHolder(View view) {

            ButterKnife.bind(this, view);
        }

        @Bind(R.id.llItem)
        LinearLayout llItem;
        @Bind(R.id.tv_use_duration)
        TextView tvUserDuration;
        @Bind(R.id.tv_user_name)
        TextView tvUserName;

        @Bind(R.id.tv_contact)
        TextView tvContact;
        @Bind(R.id.tv_authorize_time)
        TextView tvAuthorizeTime;

        @Bind(R.id.btn_terminate_authorization)
        Button btnTerminateAuthor;

        @Bind(R.id.ll_termiante)
        LinearLayout llTerminate;

    }

}

